﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NSoup;
using NSoup.Nodes;
using NSoup.Helper;
using System.Net.Http;
using System.Net;

namespace SEORank.Test
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
        }

        private void buttonTest_Click(object sender, EventArgs e)
        {
            string html = "", error = "";
            string url = "https://www.google.com/search?q=%E6%97%A0%E9%94%A1%E7%BD%91%E7%AB%99%E5%BB%BA%E8%AE%BE&start=0&num=30";
            //url = "https://www.baidu.com/s?wd=%E6%97%A0%E9%94%A1%E7%BD%91%E7%AB%99%E5%BB%BA%E8%AE%BE";
            //url = "https://www.google.com/";
            //url = "http://1212.ip138.com/ic.asp";
            //url = "http://www.google.com/";

            Proxy proxy = null;

            //SEORank.HttpHelper.UseProxy = true;

            while (string.IsNullOrWhiteSpace(html))
            {
                //获取一个代理服务器
                proxy = ProxyHelper.GetProxy();
                if (proxy == null) break;

                Console.WriteLine(proxy.Host + ":" + proxy.Port.ToString());

                //查看当前ip地址
                var ip = HttpHelper.GetIP(proxy);

                //判断是隐藏本地ip
                if (ip == null) // || (ip != null && ip.Address != proxy.Host)
                {
                    proxy.Enabled = false;
                    continue;
                }

                Console.WriteLine(ip.IP + "," + ip.Address);

                //通过代理获取url内容
                html = HttpHelper.Get(url, out error, proxy);

                if (!string.IsNullOrWhiteSpace(error))
                {
                    //MessageBox.Show(error);
                    proxy.Enabled = false;
                }
                else
                {
                    System.IO.File.WriteAllText(Application.StartupPath + @"\engine.html", html);
                }
            }



            //IConnection conn = HttpConnection.Connect(url);

            //Document doc = conn.Get();
            //html = doc.Html();
            //System.IO.File.WriteAllText(Application.StartupPath + @"\engine.html", html);



            ////conn.Url("https://www.google.com/search?q=%E6%97%A0%E9%94%A1%E7%BD%91%E7%AB%99%E5%BB%BA%E8%AE%BE&start=10");

            ////doc = conn.Get();
            ////html = doc.Html();
            ////System.IO.File.WriteAllText(Application.StartupPath + @"\engine2.html", html);




            //Document doc = NSoupClient.Parse(new Uri(url), 10 * 1000);

            //doc.GetElementById("lst-ib").Val("无锡网站建设");
            //doc.GetElementsByAttributeValue("type", "submit").e


            //webBrowser.ScriptErrorsSuppressed = false;
            //webBrowser.Navigate(url);




            //HttpClient client = new HttpClient() { BaseAddress = new Uri("https://www.google.com/") };

            //client.DefaultRequestHeaders.Add("Accept", "*/*");
            //client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, sdch, br");
            //client.DefaultRequestHeaders.Add("Accept-Language", "zh-CN,zh;q=0.8,ja;q=0.6,zh-TW;q=0.4,en;q=0.2");
            //client.DefaultRequestHeaders.Add("Connection", "keep-alive");
            ////client.DefaultRequestHeaders.Add("Host", "www.baidu.com");
            //client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");


            //var task = client.GetAsync(url);
            //html = task.Result.Content.ReadAsStringAsync().Result;
            //System.IO.File.WriteAllText(Application.StartupPath + @"\engine.html", html);

            MessageBox.Show("END" + Application.StartupPath);
        }
    }
}
